package org.iteam.core.service.base;

import java.sql.SQLException;
import java.util.List;
import org.iteam.core.Constant;
import org.iteam.core.model.base.BaseIndustry;
import org.iteam.core.service.BaseParentServiceInterface;
import org.iteam.core.service.BaseService;
import org.iteam.core.service.oracle.impl.base.BaseIndustryServiceImpl;
import org.iteam.core.utils.SqlBuilder;

public abstract class BaseIndustryService extends BaseService<BaseIndustry> implements BaseParentServiceInterface<BaseIndustry> {
	
	private static BaseIndustryService serviceMysql = null;
	private static BaseIndustryService serviceOracle = null;
	private static String tableName="BASE_INDUSTRY";

	public static BaseIndustryService getInstance() {
		Constant.DataBaseType dbTypes = Constant.dataBaseType;
		if (dbTypes.equals(Constant.DataBaseType.MYSQL)) {
			return getMySqlInstance();
		} else if (dbTypes.equals(Constant.DataBaseType.ORACLE)) {
			return getOracleInstance();
		}
		return null;
	}

	public static BaseIndustryService getMySqlInstance() {
		if (serviceMysql == null)
			serviceMysql = new org.iteam.core.service.mysql.impl.base.BaseIndustryServiceImpl();
		return serviceMysql;
	}

	public static BaseIndustryService getOracleInstance() {
		if (serviceOracle == null)
			serviceOracle = new BaseIndustryServiceImpl();
		return serviceOracle;
	}

	public BaseIndustryService() {
		super(BaseIndustry.class);
	}

	public List<BaseIndustry> query() throws SQLException {
		SqlBuilder sql=SqlBuilder.createQuery(tableName, "CODE,PARENT,NAME,ALIAS,DESCRIPTION");
		return super.query(sql.getSql());
	}

	
	public long count() throws SQLException {
		String sql = "SELECT COUNT(1) FROM BASE_INDUSTRY";
		return Long.parseLong(super.certain(sql)+"");
	}

	public long validate(String code) throws SQLException {
		String sql = "SELECT COUNT(1) FROM BASE_INDUSTRY WHERE CODE = ? ";
		return Long.parseLong(super.certain(sql, code)+"");
	}

	public BaseIndustry show(String code) throws SQLException {
		SqlBuilder sql=SqlBuilder.createQuery(tableName, "CODE,PARENT,NAME,ALIAS,DESCRIPTION");
		sql.addWhere("CODE", code);
		return super.show(sql.getSql(),sql.getParams());
	}

	
	public boolean create(BaseIndustry object) throws SQLException {
		SqlBuilder sb = SqlBuilder.createInsert(tableName);
		sb.addField("CODE", object.getCode());
		if (object.getParent() == null || "".equals(object.getParent())) {
			object.setParent(null);
		}
		sb.addField("PARENT", object.getParent());
		sb.addField("NAME", object.getName());
		sb.addField("ALIAS", object.getAlias());
		sb.addField("DESCRIPTION", object.getDescription());
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}

	
	public boolean update(BaseIndustry object) throws SQLException {
		SqlBuilder sb = SqlBuilder.createUpdate(tableName);
		if (object.getParent() == null || "".equals(object.getParent())) {
			object.setParent(null);
		}
		sb.addField("PARENT", object.getParent());
		sb.addField("NAME", object.getName());
		sb.addField("ALIAS", object.getAlias());
		sb.addField("DESCRIPTION", object.getDescription());
		sb.addWhere("CODE", object.getCode());
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public boolean delete(String id) throws SQLException {
		return destroy(id);
	}

	@Override
	public boolean destroy(String code) throws SQLException {
		SqlBuilder sql=SqlBuilder.createDelete(tableName);
		sql.addWhere("CODE", code);
		return super.update(sql.getSql(),sql.getParams());
	}

	@Override
	public List<BaseIndustry> query(String parent) throws SQLException {
		String SQL = "SELECT CODE,PARENT,NAME,ALIAS,DESCRIPTION FROM BASE_INDUSTRY  WHERE PARENT = ? ORDER BY CODE,NAME ";
		return super.query(SQL, parent);
	}

	@Override
	public long count(String parent) throws SQLException {
		if (parent == null || "".equals(parent)) {
			String sql = "SELECT COUNT(1) FROM BASE_INDUSTRY WHERE PARENT IS NULL ";
			return Long.parseLong(super.certain(sql)+"");
		} else {
			String sql = "SELECT COUNT(1) FROM BASE_INDUSTRY WHERE PARENT = ?";
			return Long.parseLong(super.certain(sql, parent)+"");
		}
	}
	
	/**
	 * 查询一级行业
	 * @return
	 * @throws SQLException 
	 */
	public List<BaseIndustry> queryParentIndustry() throws SQLException{
		SqlBuilder sql=SqlBuilder.createQuery(tableName, "CODE,PARENT,NAME,ALIAS,DESCRIPTION");
		sql.addWhere("PARENT IS NULL");
		return super.query(sql.getSql(),sql.getParams());
	} 
}
